<template>
  <div>
    <div class="tip-text">注意事项：平台至多保存1月内（31天）的操作日志</div>
    <Table
      ref="tableRef"
      :columns="data.columns"
      :toolbar="data.toolbar"
      :query="data.query"
      @get-list="getData"
    ></Table>
  </div>

</template>

<script lang="ts" setup name="stationList">
import Table from "@/components/table/index.vue";
import {
  pagingOperationLog,
  EXPORT_OPERATIONLOG,
} from "@/service/apis/logs";
import { exportFile } from "@/utils/utils";

const router = useRouter();

const tableRef = ref();

const emit = defineEmits(["get-update"]);
onActivated(() => {
  emit("get-update", (flag: boolean) => {
    if (flag) {
      tableRef.value?.getData();
    }
  });
});

const data = reactive({
  columns: [
    {
      prop: "menu",
      label: "操作菜单",
    },
    {
      prop: "type",
      label: "操作类型",
    },
    {
      prop: "content",
      label: "操作内容",
    },
    {
      prop: "createBy",
      label: "操作者账号",
    },
    {
      prop: "ip",
      label: "IP",
    },
    {
      prop: "createTime",
      label: "操作时间",
    },
  ],
  toolbar: [
    {
      label: "导出",
      permission: "operationLogList.export",
      icon: "icon-export",
      type: "primary",
      plain: true,
      handle: (row: any) => {
        const form = tableRef.value?.getForm();
        if (form?.time?.length > 0) {
          form.startTime = form.time[0];
          form.endTime = form.time[1];
        }
        exportFile(EXPORT_OPERATIONLOG, '操作日志', form);
      },
    },
  ],
  query: [
    {
      label: "操作菜单",
      prop: "menu",
      comName: "input",
      props: {
        placeholder: "请输入操作菜单",
      },
    },
    {
      label: "操作类型",
      prop: "type",
      comName: "input",
      props: {
        placeholder: "请输入操作类型",
      },
    },
    {
      label: "操作者账号",
      prop: "createBy",
      comName: "input",
      props: {
        placeholder: "请输入操作者账号",
      },
    },
    {
      label: "操作时间",
      prop: "time",
      comName: "date",
      keys: ["startTime", "endTime"],
      props: {
        type: "daterange",
      },
    },
    {
      label: "IP",
      prop: "ip",
      comName: "input",
      props: {
        placeholder: "请输入IP",
      },
    },
  ],
});

const getData = (page: any, search: any) => {
  const form: any = search.ruleForm || {};
  if (form?.time?.length > 0) {
    form.startTime = form.time[0];
    form.endTime = form.time[1];
  } else {
    form.startTime = null;
    form.endTime = null;
  }
  pagingOperationLog(form, page).then((res: any) => {
    if (res) {
      if (page.pageNo !== 1 && (!res.dataList || res.dataList?.length === 0)) {
        page.pageNo--;
        getData(page, search);
        return;
      }
      nextTick(() => {
        tableRef.value?.setTable({
          total: res.totalCount,
          table: res.dataList,
        });
      });
    }
  });
};
</script>
<style lang="scss" scoped>
.tip-text {
  font-size: #333333;
  font-size: 14px;
  font-weight: 400;
  padding: 12px 24px 12px 42px;
  background-color: rgb(198, 226, 255);
  border-radius: 12px;
}
</style>